/*
 * Copyright (C) 2011 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.database.pojo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import org.w3c.dom.Element;

/**
 * @author fred
 */
@Entity
public class Workstation implements DatabasePojo {

	public static final String GENERIC_WORKSTATION = "Workstation";

	private boolean active;
	private String comment;
	private int id;
	private Location location;
	private String name;

	/**
	 * 
	 */
	public Workstation() {
		// empty
	}

	/**
	 * @param name
	 */
	public Workstation(final String name, final Location location) {
		this.name = name;
		this.active = true;
		this.location = location;
	}

	@Override
	public void attachAttributes(final Element element) {
		element.setAttribute("id", Integer.toString(this.getId()));
		element.setAttribute("active", Boolean.toString(this.isActive()));
		element.setAttribute("location", Integer.toString(this.getLocation().getId()));
		element.setAttribute("name", this.getName());
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Workstation)) {
			return false;
		}
		final Workstation other = (Workstation) obj;
		if (this.isActive() != other.isActive()) {
			return false;
		}
		if (this.getLocation() == null) {
			if (other.getLocation() != null) {
				return false;
			}
		} else if (!this.getLocation().equals(other.getLocation())) {
			return false;
		}
		if (this.getName() == null) {
			if (other.getName() != null) {
				return false;
			}
		} else if (!this.getName().equals(other.getName())) {
			return false;
		}
		return true;
	}

	public String getComment() {
		return this.comment;
	}

	/**
	 * @return the workstation ID
	 */
	@Override
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return this.id;
	}

	/**
	 * @return the location
	 */
	@ManyToOne
	public Location getLocation() {
		return this.location;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return this.name;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (this.isActive() ? 1231 : 1237);
		result = prime * result + (this.getLocation() == null ? 0 : this.getLocation().hashCode());
		result = prime * result + (this.getName() == null ? 0 : this.getName().hashCode());
		return result;
	}

	/**
	 * @return the active
	 */
	@Override
	public boolean isActive() {
		return this.active;
	}

	/**
	 * @param active
	 *            the active to set
	 */
	public void setActive(final boolean active) {
		this.active = active;
	}

	public void setComment(final String comment) {
		this.comment = comment;
	}

	/**
	 * @param workstationId
	 *            the orgId to set
	 */
	public void setId(final int workstationId) {
		this.id = workstationId;
	}

	/**
	 * @param location
	 *            the location to set
	 */
	public void setLocation(final Location location) {
		this.location = location;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(final String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return this.getName();
	}
}
